// subpackage/pay/index.js
var e = require("../../@babel/runtime/helpers/interopRequireDefault").default,
  t = require("../../@babel/runtime/helpers/objectSpread2"),
  r = require("../../@babel/runtime/helpers/regeneratorRuntime"),
  n = require("../../@babel/runtime/helpers/slicedToArray"),
  i = require("../../@babel/runtime/helpers/asyncToGenerator"),
  a = require("../../components/base/m-button/index"),
  d = require("../../config/index"),
  o = require("../../utils/index"),
  u = e(require("./behavior")),
  s = o.helper.publish;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    info: {},
    paidAmount: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.id) {
      this.setData({
        id: options.id
      })
      $.request['GET/business/engineering/detail/' + this.data.id]().then(res => {
        res = res[0]
        if (res.code == 20002) {
          this.setData({
            info: res.data
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg
          })
        }
      })
    }
  },
  onSubmit: (0, a.apply)(i(r().mark((function e() {
    var n, i, a, d, o, u, c, l, m, p, _, h, f, v, g, b;
    return r().wrap((function (e) {
      switch (e.prev = e.next) {
        case 0:
          return e.next = 7
        case 2:
          if (a = e.sent, !this.data.isShowCode || a.code) {
            e.next = 5;
            break
          }
          return e.abrupt("return", void $.msg("请输入验证码"));
        case 5:
          if (a = e.sent, a.detail && !((null === (n = a.detail) || void 0 === n ? void 0 : n.length) < 5)) {
            e.next = 7;
            break
          }
          return e.abrupt("return", void $.msg("设备简介必须大于5个字符"));
        case 7:
          a = e.sent;
          this.getPayInfo();
          e.next = 15;
          break;
        case 13:
          return e.next = 15, s.checkPayReleasePopup(f, (function (e) {
            e ? (v.pay_code = "10001", v.coin = _.push_consume_coin, s.sendMachineInfo(v)) : s.sendMachineInfo(v)
          }));
        case 15:
        case "end":
          return e.stop()
      }
    }), e, this)
  })))),
  allPay() {
    this.setData({
      paidAmount: this.data.info.totalAmount - this.data.info.paidAmount
    })
  },
  getPayInfo(e) {
    if (!this.data.paidAmount) {
      wx.showToast({
        icon: "none",
        title: '请输入付款金额',
      })
      return false
    }
    $.request['POST/business/engineering/pay/' + this.data.id]({
      paidAmount: this.data.paidAmount
    }).then(res => {
      res = res[0]
      if (res.code == 20002) {
        wx.requestPayment({
          timeStamp: res.data.payResponse.timeStamp, // 时间戳
          nonceStr: res.data.payResponse.nonceStr, // 随机字符串
          package: res.data.payResponse.packageVal, // 统一下单接口返回的prepay_id参数值，格式为prepay_id=xxx
          signType: res.data.payResponse.signType, // 签名算法，目前支持HMAC-SHA256和MD5，默认为MD5
          paySign: res.data.payResponse.paySign, // 签名
          success(response) {
            wx.showToast({
              icon: "none",
              title: '支付成功',
            })
            setTimeout(() => {
              wx.navigateBack()
            }, 500);
            // $.request['POST/business/combine/order/payOk/' + res.data.ordeId]().then(res => {})
            // 支付成功，处理支付结果
          },
          fail(response) {
            console.log(response)
            wx.showToast({
              icon: "none",
              title: '支付失败',
            })
            $.request['POST/business/combine/order/payFail/' + res.data.ordeId]().then(res => {})
            // 支付失败，处理支付结果
          }
        })
      } else {
        wx.showToast({
          icon: 'none',
          title: res.msg
        })
      }

    })
  },
  pay() {
    wx.requestPayment({
      timeStamp: '', // 时间戳
      nonceStr: '', // 随机字符串
      package: '', // 统一下单接口返回的prepay_id参数值，格式为prepay_id=xxx
      signType: 'MD5', // 签名算法，目前支持HMAC-SHA256和MD5，默认为MD5
      paySign: '', // 签名
      success(res) {
        console.log(res)
        // 支付成功，处理支付结果
      },
      fail(res) {
        console.log(res)
        // 支付失败，处理支付结果
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})